package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.TaskDTO;
import com.k2data.k2app.domain.vo.Model;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分析模型
 *
 * @author clb
 * @date 2017年12月07日17:03:53
 */
@Api(description = "分析模型")
@RestController
@RequestMapping("/models")
public class ModelController {
	@Autowired
	private ModelService modelService;

	@ApiOperation(value = "简单模型")
	@GetMapping(value = "/simple")
	public ResponseEntity<CommonResultResponse> modelSimple() throws Exception {
		return ResponseHelper.success(modelService.listSimple());
	}

	@ApiOperation(value = "模型")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "deviceId", value = "设备id", required = false, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "noGroup", value = "是否需要按照类型分组,默认分组",required = false, dataType = "Boolean", paramType = "query")
	})

	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> models(@RequestParam(value = "deviceId", required = false) Long deviceId,
													   @RequestParam(value = "noGroup", required = false) boolean noGroup) throws Exception {

		List list;
		if(deviceId == null){
			list = modelService.list(noGroup);
		}else {
			List<Model> modelsByDevice = modelService.listModelsByDevice(deviceId);
			if (noGroup) {
				list = modelsByDevice;
			} else {
				list = modelService.listGroupByType(modelsByDevice);
			}
		}
		return ResponseHelper.success(list);
	}

	@ApiOperation(value = "模型")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> modelsById(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(modelService.get(id,true));
	}

	@ApiOperation(value = "模型统计信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "当前登陆用户", dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "模型id", required = true, dataType = "Long", paramType = "path")
	})
	@GetMapping(value = "/{id}/statistical")
	public ResponseEntity<CommonResultResponse> statInfo(@PathVariable("id") Long id,
														 @RequestParam(value = "userId", required = false) Long userId) throws Exception {
		return ResponseHelper.success(modelService.getCount(id,userId));
	}

	@ApiOperation(value = "模型的设备")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}/devices")
	public ResponseEntity<CommonResultResponse> devicesById(@PathVariable("id") Long id) throws Exception {
		List devices = modelService.listDevices(id);
		return ResponseHelper.success(devices);
	}

	@ApiOperation(value = "模型的作业")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}/jobs")
	public ResponseEntity<CommonResultResponse> jobsById(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(modelService.listJobs(id));
	}

	@ApiOperation(value = "模型的任务")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	})
	@GetMapping(value = "/{id}/tasks")
	public ResponseEntity<CommonResultResponse> tasksById(@RequestParam(value = "pageNum", required = false) Integer pageNum,
														  @RequestParam(value = "pageSize", required = false) Integer pageSize,
														  @PathVariable("id") Long id) throws Exception {

		List<TaskDTO> list = modelService.listTasks(id);
		int count = list.size();

		pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
		pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		PageHelper.startPage(pageNum, pageSize);
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);

		return ResponseHelper.success(list,pageInfo);
	}

	@ApiOperation(value = "模型的客户")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "userId", value = "当前登陆用户", dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "模型id", required = true, dataType = "Long", paramType = "path")
	})
	@GetMapping(value = "/{id}/customers")
	public ResponseEntity<CommonResultResponse> customersById(@PathVariable("id") Long id,
															  @RequestParam(value = "userId", required = false) Long userId) throws Exception {
		return ResponseHelper.success(modelService.listCustomers(id,userId));
	}

	@ApiOperation(value = "模型接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "isTemplate", value = "是否是模板模型", required = false, dataType = "Boolean", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "String", paramType = "path")
	})
	@GetMapping(value = "/{id}/interfaces")
	public ResponseEntity<CommonResultResponse> getInterfaces(@RequestParam(value = "pageNum", required = false) Integer pageNum,
															  @RequestParam(value = "pageSize", required = false) Integer pageSize,
															  @PathVariable("id") Long id,
															  @RequestParam(value = "isTemplate",required = false) Boolean isTemplate) throws Exception {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		if (pageNum != null) {
			PageHelper.startPage(pageNum, pageSize);
		}
		isTemplate = (isTemplate == null) ? Boolean.valueOf(true): isTemplate;
		List list = modelService.getResult(isTemplate,id,pageNum,pageSize);
		int count = list.size();
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
		return ResponseHelper.success(list,pageInfo);
	}

	@ApiOperation(value = "模型是否禁用")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "the value of model", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "useful", value = "操作类型", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "isTemplate", value = "是否模板模型", dataType = "Boolean", paramType = "query")
	})
	@PutMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> useful(@PathVariable("id") Long id,
													   @RequestParam(value = "useful", required = false) Boolean useful,
													   @RequestParam(value = "isTemplate", required = false) Boolean isTemplate) throws Exception {
		return ResponseHelper.success(modelService.updateUseFul(id,useful,isTemplate));
	}
}
